package pbc.gx.SaveJson.kafka.kafka2redis;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import pbc.gx.SaveJson.kafka.service.SaveJsonService;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

@Slf4j
public class Save2MysqlThread extends  Thread {
    private final JdbcTemplate jdbcTemplate;  //这个是系统自带的
    private final ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();

    private final List<ConsumerRecord<String, JSONObject>> records;

    public Save2MysqlThread(JdbcTemplate jdbcTemplate, List<ConsumerRecord<String, JSONObject>> records) {
        this.jdbcTemplate = jdbcTemplate;
        this.records = records;
    }

    @Transactional
    public void run() {

        SaveJsonService saveJsonService = applicationContext.getBean(SaveJsonService.class);
        saveJsonService.save2Mysql(jdbcTemplate, records);
        //log.info("Mysql:" + this.getId()+" "+records.size());
    }
}
